package com.wb.oldwomen.module;

import android.util.Log;

import com.wb.fw.WorldCamera;
import com.wb.fw.WorldObject;
import com.wb.fw.Global;
import com.wb.oldwomen.GamePlayLayer;
import com.wb.oldwomen.Ioldwomam;
import com.wb.oldwomen.R;
import com.wiyun.engine.actions.Action;
import com.wiyun.engine.actions.JumpTo;
import com.wiyun.engine.actions.MoveBy;
import com.wiyun.engine.actions.MoveTo;
import com.wiyun.engine.actions.Spawn;
import com.wiyun.engine.actions.Action.Callback;
import com.wiyun.engine.actions.JumpBy;
import com.wiyun.engine.actions.RepeatForever;
import com.wiyun.engine.actions.RotateBy;
import com.wiyun.engine.afcanim.MWSprite;
import com.wiyun.engine.nodes.Scheduler;
import com.wiyun.engine.nodes.Timer;
import com.wiyun.engine.opengl.Texture2D;
import com.wiyun.engine.types.WYRect;

public class Enemy extends WorldObject implements Ioldwomam {
	MWSprite msprite;
	BaseConfigData[] mactions;
//	int[] mactionPos;
	int mcurAction;
//	RepeatForever mrf;
//	float mrunDis, mlastRunDis;
	float mspeed;
	public static Enemy make(GamePlayLayer mparent) {
		return new Enemy(mparent);
	}
	
	protected Enemy(GamePlayLayer mparent) {
		Texture2D tex = Texture2D.makePNG(R.drawable.enemy);
		tex.autoRelease();
		msprite = MWSprite.make(R.raw.enemy, ENEMY_ANIM_START, tex );
//		msprite.setDebugDrawFrameRect(true);
		msprite.setScale(Global.sScaleRate);
		msprite.autoRelease();
		msprite.setUnitInterval(0.06f);
		
//		MoveBy mb = MoveBy.make(1.0f, 25*ENEMY_SPEED, 0);
//		mb.autoRelease();
//		mrf = RepeatForever.make(mb);
//		mb.autoRelease();
		
		mparent.addChild(msprite, Z_ROAD);
	}
	
	public void setLevelData(int level) {
//		mstop = false;
		mspeed = ENEMY_X_SPEED;
		mcurAction = 0;
		msprite.playAnimation(ENEMY_ANIM_START);
//		Log.d("xltest", "getLoopCount="+msprite.getLoopCount());
		mposx = ENEMY_FOCUS_X;
		mposy = Y_ROAD_RUN-20;
		msprite.setPosition(mposx*Global.sScaleRate, mposy*Global.sScaleRate);
		
		String[] data = ENEMY_LEVEL_CONFIG[level].split(",");
		int cur = 0;
		mactions =  new BaseConfigData[data.length/3];
		for (int i=0;i<mactions.length;i++) {
			mactions[i] = new BaseConfigData();
			mactions[i].mname = data[cur++];
			mactions[i].mdistance = Integer.valueOf(data[cur++]);
			mactions[i].mheight = Integer.valueOf(data[cur++]);
		}
	}
	
	public void clearLevelData() {
		if (mactions!= null) {
			for(int i=0;i<mactions.length;i++) {
				mactions[i] = null;
			}
			mactions = null;
		}
		mcurAction = 0;
//		mstop = false;
	}
	
	public void startRun() {
		Log.d("xltest", "getAnimationCount="+msprite.getAnimationCount());
		msprite.setLoopCount(-1);
		msprite.playAnimation(ENEMY_ANIM_RUN);
//		msprite.runAction(mrf);
	}
	
	public void update(float delta) {
//		if (mstop) {
//			if (mstopDis <= 0) {
//				return;
//			}
//			mstopDis -= ENEMY_X_SPEED;
//		}
		mposx += mspeed;
		msprite.tick(delta);
		msprite.setPosition((mposx-WorldCamera.getInstance().getCameraPosx())*Global.sScaleRate, msprite.getPositionY());
		WYRect spriterect = msprite.getFrameRect();//getCollisionRectRelativeToWorld(0);
		if (mcurAction < mactions.length
				&& spriterect.containsPoint(mactions[mcurAction].mdistance
						- mposx, 10)) {
			// Log.d("xltest",
			// "collision mcurAction="+mcurAction+",msprite.x="+msprite.getPositionX()+",mactionPos[mcurAction]="+mactionPos[mcurAction]);
			if (mactions[mcurAction].mname.equals("jump")) {
				int height = mactions[mcurAction].mheight;
				float duration = height/ENEMY_JUMP_Y_SPEEDR_PER_SEC;
				JumpTo jb = JumpTo
						.make(duration, msprite.getPositionX(),
								msprite.getPositionY(), msprite.getPositionX(),
								msprite.getPositionY(),
								mactions[mcurAction].mheight*Global.sScaleRate, 1);
				jb.autoRelease();
//				RotateBy rb = RotateBy.make(2.0f, 360);
//				Spawn spawn = Spawn.make(jb, rb);
				msprite.runAction(jb);
				// msprite.runAction(mactions[mcurAction]);
				mcurAction++;
			}
		}
//		msprite.playAnimation(mcurAction);
		
		computerWorldCollisionRect();
	}
	void computerWorldCollisionRect() {
		WYRect rect = msprite.getFrameRect();
		if (rect != null) {
//		Log.d("xltest", "collision rect="+rect.minX()+",my="+rect.minY()+",ax="+rect.maxX()+",ay="+rect.maxY());
			mcollisionRect = WYRect.make(mposx + rect.minX(),
					mposy + rect.minY(), rect.maxX() - rect.minX(), rect.maxY()
							- rect.minY());
//		Log.d("xltest", "-------------collision rect="+rect.minX()+",my="+rect.minY()+",ax="+rect.maxX()+",ay="+rect.maxY());
		} else {
			mcollisionRect = null;
		}
	}
	WYRect mcollisionRect;
	/**
	 * 世界坐标的框
	 * @return
	 */
	public WYRect getWorldCollisionRect() {
		return mcollisionRect;
	}
	
//	float mstopDis;
//	boolean mstop;
	public void stop() {
		mspeed = 0;
		msprite.playAnimation(ENEMY_ANIM_STOP_JUMP);
//		mstop = true;
//		mstopDis = BASE_VERSION_SCREEN_WIDTH/3*Global.sScaleRate;
//		Scheduler.getInstance().schedule(new Timer(this, "finish", 2.0f));
	}
//	boolean finish;
//	public void finish() {
//		finish = true;
//	}
	
}
